﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using UART_PORTS;

namespace demo01
{
    public partial class MainForm : Form
    {
        int signNum = 0;
        static ComRecv_Class CR = new ComRecv_Class();
        static ComRecv_Class DisplayData = new ComRecv_Class();
        UInt32 CountValue = 0;
        byte[] Data = new byte[8];

        public Queue<double> dataQueue = new Queue<double>(1000);
        public int curValue = 0;
        public int num = 5;//每次删除增加几个点
        public MainForm()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        //关闭串口
        private void test1_Click(object sender, EventArgs e)
        {
            // outtextBox.Text = "这是测试二按钮";
            toolStripStatusLabel2.Text = "串口状态已经关闭";
            serialPort1.Close();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        //打开串口
        private void test2_Click(object sender, EventArgs e)
        {
            // outtextBox.Text = "这是测试二按钮";
            toolStripStatusLabel2.Text = "串口状态已经打开";
            serialPort1.Open();

        }

        private void test3_Click(object sender, EventArgs e)
        {
            outtextBox.Text = "这是测试三按钮";
            //  ContentPanel= "ok!显示数据成功";
            // toolStripContainer1.Text = "ok!显示数据成功";
            toolStripStatusLabel1.Text = "ok!显示数据成功";
        }

        //========================程序入口
        private void MainForm_Load(object sender, EventArgs e)
        {
            //启动定时器
            // timer11.Enabled = true;
            timer11.Interval = 1000;

            //获取串口信息
            //  string[] str = serialPort1.GetPortNames();
            string[] str = SerialPort.GetPortNames();

            outtextBox.Text = serialPort1.PortName;

            if (str == null)
            {
                toolStripStatusLabel2.Text = "本机没有串口！";
                return;
            }

            //添加串口项目

            //串口默认选择项

            //接收数据
            try
            {
                Byte[] receivedData = new Byte[serialPort1.BytesToRead];        //创建接收字节数组
                serialPort1.Read(receivedData, 0, receivedData.Length);         //读取数据                       
                serialPort1.DiscardInBuffer();                                  //清空SerialPort控件的Buffer
                string strRcv = null;
                for (int i = 0; i < receivedData.Length; i++) //窗体显示
                {

                    strRcv += receivedData[i].ToString("X2");  //16进制显示
                }
                outtextBox.Text += strRcv + "\r\n";
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "出错提示");
                outtextBox.Text = "";
            }
        }

        public void fun00()
        {
            outtextBox.AppendText("fun00\n");
        }
        //10秒执行
        public void fun10()
        {
            outtextBox.AppendText("fun10\n");
        }
        //20秒执行
        public void fun20()
        {
            outtextBox.AppendText("fun20\n");
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //textBox1.Text = DateTime.Now.ToString("h:mm:ss");
           // outtextBox.Text = DateTime.Now.ToString("h:mm:ss");
            textBox1.Text = signNum.ToString();
            if (signNum >= 0 && signNum < 10)
            {
                fun00();
            }
            if (signNum >= 10 && signNum < 20)
            {
                fun10();
            }
            if (signNum >= 20 && signNum < 30)
            {
                fun20();
            }
            if (signNum >= 30)
            {
                timer11.Enabled = false;
                signNum = 0;
                outtextBox.AppendText("循环结束\n");
                Timerbutton.Text = "开启定时器";
            }
            signNum++;
          //  toolStripStatusLabel1.Text = "时间：" + DateTime.Now.ToString("h:mm:ss");
        }

        //接收
        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            int n = serialPort1.BytesToRead;
            if (CR.pWrite + n > CR.MaxLength)
            {
                serialPort1.Read(CR.ComRecvBuf, CR.pWrite, CR.MaxLength - CR.pWrite);
                serialPort1.Read(CR.ComRecvBuf, 0, CR.pWrite + n - CR.MaxLength);
            }
            else
            {
                serialPort1.Read(CR.ComRecvBuf, CR.pWrite, n);
            }
            CR.pWrite = (UInt16)(CR.pWrite + n);
            CR.pWrite %= CR.MaxLength;
            //if ((((CR.pWrite + CR.MaxLength) - CR.pRead) % CR.MaxLength) >= CR.MinLen)
            ComProcess();
            //
        }

        //处理程序
        void ComProcess()
        {
            byte[] dataBuf = new byte[CR.MinLen];  // 定义临时数组
            if ((((CR.pWrite + CR.MaxLength) - CR.pRead) % CR.MaxLength) >= CR.MinLen)  // 当长度一定后开始处理
                do
                {
                    for (byte i = 0; i < CR.MinLen; i++)
                    {
                        dataBuf[i] = CR.ComRecvBuf[(CR.pRead + i) % CR.MaxLength];
                    }
                    if ((dataBuf[0] == 0xAA) && (dataBuf[1] == 0x55) && (dataBuf[CR.MinLen - 1] == 0x88))
                    {
                        if (GetSumForCommand(dataBuf) == dataBuf[28])  // 为了提升性能，这个和检验功能去掉
                        {
                            switch (dataBuf[2])
                            {
                                case 1:   // 根据协议来，暂时不做解析！
                                    UInt32 BaduRate = (UInt32)((dataBuf[10] << 16) + (dataBuf[11] << 8) + (dataBuf[12]));
                                    outtextBox.Text = BaduRate.ToString();
                                    break;
                                case 2:   // 这个报文只有接收成功计数
                                    CountValue = (UInt32)((dataBuf[3] << 24) + (dataBuf[4] << 16) + (dataBuf[5] << 8) + dataBuf[6]);
                                    outtextBox.Text = CountValue.ToString();
                                    break;
                                case 3:   // 这个为接收的CAN报文
                                    // 先把有用信息提取出来
                                    UInt32 RIR = (UInt32)((dataBuf[3] << 24) + (dataBuf[4] << 16) + (dataBuf[5] << 8) + dataBuf[6]);
                                    UInt32 RDTR = (UInt32)((dataBuf[7] << 24) + (dataBuf[8] << 16) + (dataBuf[9] << 8) + dataBuf[10]);
                                    UInt32 RDHR = (UInt32)((dataBuf[11] << 24) + (dataBuf[12] << 16) + (dataBuf[13] << 8) + dataBuf[14]);
                                    UInt32 RDLR = (UInt32)((dataBuf[15] << 24) + (dataBuf[16] << 16) + (dataBuf[17] << 8) + dataBuf[18]);
                                    // 然后寄存器手册来操作！这里只做几个简单的演示！
                                    {
                                        if ((RIR & 0x6) == 0)  // 表示为标准帧，数据帧
                                        {
                                            if ((RDTR & 0x0F) == 0x08)  //表示数据长度为8位
                                            {

                                                Data[0] = (byte)(RDLR);
                                                Data[1] = (byte)(RDLR >> 8);
                                                Data[2] = (byte)(RDLR >> 16);
                                                Data[3] = (byte)(RDLR >> 24);
                                                Data[4] = (byte)(RDHR);
                                                Data[5] = (byte)(RDHR >> 8);
                                                Data[6] = (byte)(RDHR >> 16);
                                                Data[7] = (byte)(RDHR >> 24);

                                                try
                                                {
                                                    DisplayData.ComRecvBuf[DisplayData.pWrite++] = (byte)(Data[7] % 100);
                                                    DisplayData.pWrite %= DisplayData.MaxLength;
                                                    if ((((DisplayData.pWrite + DisplayData.MaxLength) - DisplayData.pRead) % DisplayData.MaxLength) >= 100)
                                                    {
                                                        DisplayData.pRead++;
                                                        DisplayData.pRead %= DisplayData.MaxLength;
                                                    }
                                                }
                                                catch
                                                {

                                                }

                                            }
                                        }
                                    }

                                    break;
                                default:
                                    break;
                            }
                            CR.pRead = (UInt16)(CR.pRead + CR.MinLen);
                            CR.pRead %= CR.MaxLength;
                        }
                        else
                        {
                            CR.pRead++;
                            CR.pRead %= CR.MaxLength;
                        }
                    }
                    else
                    {
                        CR.pRead++;
                        CR.pRead %= CR.MaxLength;
                    }

                } while ((((CR.pWrite + CR.MaxLength) - CR.pRead) % CR.MaxLength) >= CR.MinLen);
        }
        //专用求和函数
        public byte GetSumForCommand(byte[] Array)  // 专用求和函数
        {
            byte Sum = 0;
            for (byte temp = 0; temp <= 27; temp++)
            {
                Sum = (byte)(Sum + Array[temp]);
            }
            return Sum;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void 号ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            outtextBox.BackColor = Color.FromArgb(128, 255, 128);
        }

        private void 号ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            outtextBox.BackColor = Color.FromArgb(255, 128, 128);
        }

        private void 号ToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            outtextBox.BackColor = Color.FromArgb(255, 255, 128);
        }

        private void 弹出对话ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //if (panel1.Visible == false)
            //{
            //    panel1.Visible = true;
            //    return;
            //}
            //if (panel1.Visible == true)
            //{
            //    panel1.Visible = false;
            //    return;
            //}

        
        }

        private void 面板2ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //if (panel2.Visible == false)
            //{
            //    panel2.Visible = true;
            //    return;
            //}
            //if (panel2.Visible == true)
            //{
            //    panel2.Visible = false;
            //    return;
            //}
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 form = new Form2();
            form.Show();
            //form.ShowDialog();

           
        }

        private void Timerbutton_Click(object sender, EventArgs e)
        {
            if (timer11.Enabled == true)
            {
                timer11.Enabled =false;
                signNum = 0;
                Timerbutton.Text = "开启定时器";
            }
            else
            {
                timer11.Enabled = true;
                Timerbutton.Text = "关闭定时器";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }


        //end
    }
}
